﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebShop.Models;

namespace WebShop.Controllers
{
    [Authorize]
    public class BasketController : Controller
    {
        private WGRUSEntities db = new WGRUSEntities();
        //
        // GET: /Basket/

        public ActionResult Index()
        {
            UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName == User.Identity.Name);
            return View(db.Baskets.Where(b => b.Customer_Id == user.Customer_Id).ToList());
        }

        //
        // GET: /Basket/Details/5

        public ActionResult Details(int id = 0)
        {
            Basket basket = db.Baskets.Find(id);
            if (basket == null)
            {
                return HttpNotFound();
            }
            return View(basket);
        }


        //
        // GET: /Basket/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Basket basket = db.Baskets.Find(id);
            if (basket == null)
            {
                return HttpNotFound();
            }
            return View(basket);
        }

        //
        // POST: /Basket/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Basket basket)
        {
            if (ModelState.IsValid)
            {
                db.Entry(basket).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(basket);
        }

        //
        // GET: /Basket/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Basket basket = db.Baskets.Find(id);
            if (basket == null)
            {
                return HttpNotFound();
            }
            return View(basket);
        }

        //
        // POST: /Basket/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Basket basket = db.Baskets.Find(id);
            db.Baskets.Remove(basket);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}